import cTitle from "components/title";
import { Toast } from "vant";
import payKeyboard from "components/payKeyboard";
import { payKeyboardAsync } from "utils/payKeyboard";
import diyBackGround from "./componet/diyBackGround";

export default {
  data() {
    return {
      toi: this.fun.getKeyByI(),
      balance: 0,
      info_form: {
        transfer_id: "",
        transfer_money: ""
      },
      transfer_id: "",
      memberInfo: "",
      //余额字样
      balanceLang: this.fun.getBalanceLang(),

      payKeyboardShow: false, //键盘显示
      need_password: false, //是否需要验证支付密码
      has_password: false, //是否设置了支付密码

      showConfirm: false, //确认转账弹窗
      deduct_point:{},
      transfer_deduct_point: {},
      memberPoundage: {}, //会员等级手续费比例
    };
  },
  watch: {
    async transfer_id(new_transfer_id) {
      let { result, msg, data } = await $http.get(
        "member.member.memberInfo",
        {
          uid: this.transfer_id
        },
        "."
      );
      if (result == 0) return this.$toast(msg);
      this.memberInfo = data;
      this.getMemberPoundage();
    }
  },
  methods: {
    async getMemberPoundage() {
      //获取会员等级手续费比例
      if(!this.balance_transfer_plugin) return;
      let { result:result2, msg: msg2, data:data2 } = await $http.get(
        "plugin.balance-transfer.frontend.transfer.get-member-poundage",
        {
          recipient: this.transfer_id
        },
        "."
      );
      if (result2 == 0) return this.$toast(msg2);
      this.memberPoundage = data2 || {};
    },
    beforeConfirm() {
      //确认转账前处理
      if (parseFloat(this.info_form.transfer_money) > parseFloat(this.balance)) {
        this.$dialog.alert({ message: "转让金额不可大于您的"+this.balanceLang });
        return;
      }
      if (this.transfer_id == undefined || this.transfer_id <= 0 || this.transfer_id.length == 0) {
        this.$dialog.alert({ message: "转让id不可为空" });
        return;
      }
      if (this.info_form.transfer_money == undefined || this.info_form.transfer_money <= 0 || this.info_form.length == 0) {
        this.$dialog.alert({ message: "转让金额不可低于0元" });
        return;
      }

      if (this.need_password == true && this.has_password == false) {
        this.$dialog.confirm({ message: "请先设置支付密码" }).then(() => {
          this.$router.push(this.fun.getUrl("set_balance_password", {}));
        });
        return;
      }
      this.showConfirm = true;
    },
    //确认转账
    async confirm() {
      var that = this;
      if(this.lockClick) return;//防止重复点击
      this.lockClick = true;

      let json = {
        recipient: this.transfer_id,
        transfer_money: this.info_form.transfer_money
      };
      if (this.need_password == true) {
        //开启支付密码验证
        let pass = await this.getPayKeyboardPassword();
        json.password = pass;
      }
      $http.get("finance.balance.transfer", json, " ").then((response)=> {
        if (response.result === 1) {
          that.$dialog.alert({ message: response.msg }).then(() => {
            this.lockClick = false;
            that.$router.go(-1);
          });
        } else {
          this.lockClick = false;
          that.$dialog.alert({ message: response.msg });
        }
      },
      (response)=> {
        this.lockClick = false;
        // error callback
      }
      );
    },

    payComplete(text) {
      console.log(text, "页面");
      payKeyboardAsync.complete(text);
    },
    getPayKeyboardPassword() {
      this.payKeyboardShow = true;
      return new Promise((resove, reject) => {
        this.lockClick = false;
        payKeyboardAsync.addCompleteFn(pass => {
          resove(pass);
        });
      });
      // let pass = await this.getPayKeyboardPassword();
      // console.log(pass);
    },

    getBalence() {
      $http
        .get("finance.balance.member-balance", {}, " ")
        .then(response => {
          if (response.result === 1) {
            if(response.data.deduct_point){
              this.deduct_point = response.data.deduct_point;
            }
            if(response.data.balance_transfer_plugin && response.data.balance_transfer_plugin.status){
              this.balance_transfer_plugin = response.data.balance_transfer_plugin;
            }
            if(response.data.transfer_deduct_point){
              this.transfer_deduct_point = response.data.transfer_deduct_point;
            }
            this.balance = response.data.credit2;
            this.need_password = response.data.need_password;
            this.has_password = response.data.has_password;
          } else {
            this.$dialog.alert({ message: response.msg }).then(() => {
              this.$router.go(-1);
            });
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  },
  computed:{
    countDeduct(){
      let ratio = Number(this.deduct_point.deduct_point) / Number(this.deduct_point.deduct_balance) ;
      return Number(this.info_form.transfer_money * ratio).toFixed(2);
    },
    transfDeductPoi(){
      let ratio = Number(this.transfer_deduct_point.ratio) / 100 ;
      if(!this.transfer_deduct_point.status) ratio = 0;
      let memberPoundageratio = Number(this.memberPoundage.ratio) / 100 ;
      memberPoundageratio = memberPoundageratio ? memberPoundageratio : 0;
      return (Number(this.info_form.transfer_money ) + Number(this.info_form.transfer_money * ratio) + Number(this.info_form.transfer_money * memberPoundageratio)).toFixed(2);
    },
  },
  activated() {
    this.toi = this.fun.getKeyByI();
    //this.balance = this.$route.params.balance;
    this.lockClick = false;
    this.getBalence();
  },
  components: { cTitle, payKeyboard, diyBackGround }
};
